import { configureStore, combineReducers } from '@reduxjs/toolkit';
import {
  persistStore,
  persistReducer,
  FLUSH,
  REHYDRATE,
  PAUSE,
  PERSIST,
  PURGE,
  REGISTER
} from 'redux-persist'
import AsyncStorage from '@react-native-async-storage/async-storage'
import userReducer from './reducer/user'
import chatReducer from './reducer/chat';
import testReducer from './reducer/test';
import state from './state'
// import RouterReducer from './reducer/router'
import { setMsgListAction, setSocketAction } from './actionCreator/chat'
import axios from '../axios';

const persistConfig = {
  key: 'user',
  storage: AsyncStorage, //指定存储位置，一定要写
  // 白名单会持久化数据
  whitelist: ['userReducer', 'chatReducer']
}

const reducers = combineReducers({
  userReducer,
  chatReducer,
  state,
  testReducer
  // RouterReducer
})

const persistedReducer = persistReducer(persistConfig, reducers)

let store = configureStore({
  reducer: persistedReducer,
  middleware: (getDefaultMiddleware) => //不写报错
    getDefaultMiddleware({
      // serializableCheck: false,
      serializableCheck: {
        ignoredActions: [FLUSH, REHYDRATE, PAUSE, PERSIST, PURGE, REGISTER],
      },
    }),
})

let persistor = persistStore(store)

// 获取持久化存储信息
AsyncStorage.getItem('persist:user').then((data) => {
  const user = JSON.parse(JSON.parse(data).userReducer)
  // console.log('获取持久化存储信息=', user);
})


// 初始化数据
// axios.get('/friend').then(res => {
//   console.log('调用接口', res.data)
//   store.dispatch(setMsgListAction(res.data))
// }).catch((err) => { })

// 创建 Redux store 来存放应用的状态。
// API 是 { subscribe, dispatch, getState }。
export { persistor }
export default store;